package com.qingluan.server;

import com.qingluan.arguments.ARGUMENTS;
import com.qingluan.server.BroadCastRecevier.ReceiveListener;

import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class GeoService extends Service{
	IntentFilter filter ;
	BroadCastRecevier broadCast ;
	WebSocketConnection connection ;
	
	//context area
	private String tag =  this.getClass().getName();
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.d(tag, "create service for location");
		filter = new IntentFilter();
		filter.addAction(ARGUMENTS.GEO_LOCATION_SERVICE_PUSH);
		broadCast = new BroadCastRecevier();
		registerReceiver(broadCast, filter);
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		Log.d(tag, "start service for location");
		if (broadCast == null){
			Log.d(tag, "uninitialization ..broad");
			broadCast = new BroadCastRecevier();
			registerReceiver(broadCast, filter);
		}
		
		broadCast.setReceiveListenr(new ReceiveListener(){

			@Override
			public void receive(String info) {
				// TODO Auto-generated method stub
				if (info.equals("OFF")){
					if(connection.isConnected()){
						connection.disconnect();
					}
				}
				if (connection.isConnected()){
					connection.sendTextMessage(info);
				}
			}
			
		});
		startWebSocket();
		
		// this for login ...
		
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	
	private void startWebSocket(){
		connection = new WebSocketConnection();
		try {
			connection.connect(ARGUMENTS.GEO_LOCATION_IP, new WebSocketHandler(){
				
				@Override
				public void onOpen() {
					// TODO Auto-generated method stub
					Log.d(tag, "open connection "+ARGUMENTS.GEO_LOCATION_IP);
					Intent intent = new Intent();
					intent.setAction(ARGUMENTS.GEO_LOCATION_SERVICE_RECE);
					intent.putExtra(BroadCastRecevier.KEY, "ON");
					Log.d(tag, "send to ON");
					sendBroadcast(intent);
				}
				
				@Override
				public void onTextMessage(String payload) {
					// TODO Auto-generated method stub
					Log.d(tag, "get from connection "+payload);
					Intent intent = new Intent();
					intent.setAction(ARGUMENTS.GEO_LOCATION_SERVICE_RECE);
					intent.putExtra(BroadCastRecevier.KEY,payload);
					Log.d(tag, "get ?");
					sendBroadcast(intent);
				}
				
				
				@Override
				public void onClose(int code, String reason) {
					// TODO Auto-generated method stub
					Log.d(tag, "close connection");
					Intent intent =new Intent();
					intent.setAction(ARGUMENTS.GEO_LOCATION_SERVICE_RECE);
					intent.putExtra(BroadCastRecevier.KEY, "OFF");
					sendBroadcast(intent);
				}
				
			});
		} catch (WebSocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
}
